package com.shaoshaohuo.app.view;

import com.shaoshaohuo.app.R;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * @author yusonglin
 * @Des 消息dialog
 */
public class MessageDialog {

    private View btnGaps;
    private Dialog mDialog;

    private Context mContext;

    private View mContentView;

    /**
     * 消息
     */
    private TextView mMessageText;
    /**
     * 左边button
     */
    private TextView mLeftButton;
    /**
     * 右边button
     */
    private TextView mRightButton;
    /**
     * 监听器
     */
    private OnBtClickListener mListener;

    public MessageDialog(Context context) {

        this.mContext = context;

        mDialog = new Dialog(mContext,R.style.Custom_dialog_style);
        mContentView = View.inflate(mContext, R.layout.message_dialog, null);

        mMessageText = (TextView) mContentView.findViewById(R.id.txt_messageDialog_message);
        mLeftButton = (TextView) mContentView.findViewById(R.id.txt_messageDialog_leftButton);
        mRightButton = (TextView) mContentView.findViewById(R.id.txt_messageDialog_rightButton);
        btnGaps = (View) mContentView.findViewById(R.id.btn_gaps);


        WindowManager.LayoutParams localLayoutParams = mDialog.getWindow().getAttributes();
        mDialog.onWindowAttributesChanged(localLayoutParams);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setCancelable(true);
        mContentView.setMinimumWidth(10000);
        mDialog.setContentView(mContentView);
        localLayoutParams.gravity = Gravity.CENTER;
    }

    public void setCanceledOnTouchOutside(boolean canceled){
        mDialog.setCanceledOnTouchOutside(canceled);
    }

    public void setCancelable(boolean canceled){
        mDialog.setCancelable(canceled);
    }

    public void setLeftBtVisible(boolean isVisible) {
        mLeftButton.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        btnGaps.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置消息内容
     * @param message
     */
    public void setMessage(String message) {
        mMessageText.setText(message);
    }

    /**
     * 设置消息内容
     *
     * @param resId 资源Id
     */
    public void setMessage(int resId) {
        mMessageText.setText(resId);
    }

    /**
     * 设置左边button名称
     *
     * @param text
     */
    public void setLeftBtText(String text) {
        mLeftButton.setText(text);

    }

    /**
     * 设置左边button名称
     *
     * @param resId
     */
    public void setLeftBtText(int resId) {
        mLeftButton.setText(resId);
    }

    /**
     * 设置右边button名称
     *
     * @param text
     */
    public void setRightBtText(String text) {
        mRightButton.setText(text);
    }

    /**
     * 设置右边button名称
     *
     * @param resId
     */
    public void setRightBtText(int resId) {
        mRightButton.setText(resId);
    }

    /**
     * 设置监听
     *
     * @param listener
     */
    public void setOnBtClickListener(OnBtClickListener listener) {
        this.mListener = listener;
        mLeftButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mListener.onLeftBtClick();
                mDialog.dismiss();
            }
        });
        mRightButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mListener.onRightBtClick();
                mDialog.dismiss();
            }
        });
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    /**
     * @ClassName: OnBtClickListener
     */
    public interface OnBtClickListener {

        void onLeftBtClick();

        void onRightBtClick();
    }

}
